#include "vedb.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

const int FILE_NUM = 200;

void setFiles(int num, nlohmann::json& vedbJson) {
    assert(num > 0);
    using namespace nlohmann;
    json allFilesJson;
    string allFilesJsonPath = "../files.json";
    ifstream is_all_files(allFilesJsonPath);
    is_all_files >> allFilesJson;
    is_all_files.close();

    // 从全部文件集里读取前 num 个
    json& fileJson = vedbJson["files"];
    map<string, vector<int>> W;
    fileJson.clear();
    for (int i = 0; i < num; i++) {
        json file = allFilesJson[i];
        for (auto word_it = file["keywords"].begin(); word_it != file["keywords"].end(); ++word_it) {
            string word = (*word_it).get<string>();
            if (W.find(word) == W.end()) {
                W[word] = vector<int>{i};
            } else {
                W[word].push_back(i);
            }
        }
        string fileName = "file" + to_string(i);
        file["name"] = fileName;
        fileJson.push_back(file);
    }

    // 根据当前文件集设置 W
    json& wordJson = vedbJson["W"];
    for (auto w_it = W.begin(); w_it != W.end(); ++w_it) {
        wordJson[w_it->first] = w_it->second;
    }
}

int main() {

    using namespace nlohmann;
    string vedbJsonPath = "vedb.json";
    json vedbJson;
    ifstream is_vedb(vedbJsonPath);
    is_vedb >> vedbJson;

    // setFiles(200, vedbJson);
    // VEDBSetup(vedbJson);

    setSearchWords(vedbJson, 3);
    TokenGen(vedbJson);
    Search(vedbJson);
    Verify(vedbJson);

    


    ofstream o(vedbJsonPath);
    o << setw(2) << vedbJson << endl;



    // pair_class pairing("type a\nq 8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791\nh 12016012264891146079388821366740534204802954401251311822919615131047207289359704531102844802183906537786776\nr 730750818665451621361119245571504901405976559617\nexp2 159\nexp1 107\nsign1 1\nsign0 1\n");
    // ele_class g(pairing, "G1");
    // g.random();
    // cout << g << endl;
    // ele_class s(pairing, "Zr");
    // s.random();
    // BMapAcc acc(g, s);
    // vector<ele_class> vec;
    // for (int i = 1; i < 11; i++) {
    //     ele_class t(pairing, "Zr");
    //     t.random();
    //     cout << t << endl;
    //     vec.push_back(t);
    // }
    // ele_class res = acc(vec);
    // cout << res << endl;

    // ele_class lhs = acc(vec.begin(), 5);
    // ele_class rhs = acc(vec.begin() + 5, 5);
    // cout << lhs << endl;
    // cout << rhs << endl;

    // cout << (pairing(lhs, rhs) == pairing(res, g)) << endl;

}